package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    public CategoryService categoryService;
    @Autowired
    public DishService dishService;
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        save(setmealDto);
        setmealDto.getSetmealDishes().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    public Page<SetmealDto> selectByPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageInfo = new Page<>();
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        page(setmealPage,queryWrapper);
        pageInfo.setTotal(setmealPage.getTotal());
        ArrayList<SetmealDto> records = new ArrayList<>();
        setmealPage.getRecords().forEach(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            Category category = categoryService.getById(setmeal.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            BeanUtils.copyProperties(setmeal,setmealDto);
            records.add(setmealDto);
        });
        pageInfo.setRecords(records);
        return pageInfo;
    }

    @Override
    @Transactional
    public void removeWithDish(Long[] ids) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", Arrays.asList(ids));
        queryWrapper.eq("status",1);
        int count = count(queryWrapper);
        if (count > 0){
            throw new CustomerException("套餐正在售卖,不能删除");
        }
        this.removeByIds(Arrays.asList(ids));
        //删除套餐对应的菜品
        for (Long setmealId : ids) {
            QueryWrapper<SetmealDish> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("setmeal_id",setmealId);
            setmealDishService.remove(queryWrapper1);
        }

    }

    @Override
    public List<SetmealDishDto> listWithImage(Long setmealId) {
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",setmealId);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        ArrayList<SetmealDishDto> setmealDtos = new ArrayList<>();
        for (SetmealDish dish : list) {
            SetmealDishDto dishDto = new SetmealDishDto();
            BeanUtils.copyProperties(setmealDtos,dishDto);
            String image = dishService.getById(dish.getDishId()).getImage();
            dishDto.setImage(image);
            setmealDtos.add(dishDto);
        }
        return setmealDtos;
    }
}
